package com.vinjogames.som.entities;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import com.vinjogames.gfx.Camera;
import com.vinjogames.gfx.Image;
import com.vinjogames.gfx.TextureId;
import com.vinjogames.util.GameClock;

public class Player extends Character{		
		protected Image down;
		protected Image up;
		protected Image left;
		protected Image right;
		
		protected float mGLWidth = 1;
		protected float mGLHeight = 1;
		
		public int state = 0;
		
		protected int moveState = 0;
		
		public Player()
		{
			down =  new Image(TextureId.DOWN_TEXTURE_ID, mGLWidth, mGLHeight);
			down.setAnimations(new int[]{0,4,0});
			
			up =  new Image(TextureId.UP_TEXTURE_ID, mGLWidth, mGLHeight);
			up.setAnimations(new int[]{0,4,0});
			
			left =  new Image(TextureId.LEFT_TEXTURE_ID, mGLWidth, mGLHeight);
			left.setAnimations(new int[]{0,4,0});
			
			right =  new Image(TextureId.RIGHT_TEXTURE_ID, mGLWidth, mGLHeight);
			right.setAnimations(new int[]{0,4,0});

			location = new PointF(2, 2);
			lastUpdate = 0;
		}
		
		public void update(long t) {
			if(lastUpdate == 0)
				lastUpdate = t;
			switch(moveState){
			case 1:
				moveRight(t - lastUpdate);
				state = 3;
				break;
			case 2:
				moveUp(t - lastUpdate);
				state = 1;
				break;
			case 3:
				moveLeft(t - lastUpdate);
				state = 2;
				break;
			case 4:
				moveDown(t - lastUpdate);
				state = 0;
				break;
			}
			// Should probably be moved to the collision checker
			if(mGLWidth * 0.5f > location.x) // Far Left
				location.set(mGLWidth * 0.5f, location.y);
			if(mGLHeight * 0.5f > location.y) // Bottom
				location.set(location.x, mGLHeight * 0.5f);
			if(Camera.getInstance().getLeft() + Camera.getInstance().GL_WIDTH*2 - mGLWidth * 0.5f < location.x) // Far Right
				location.set(Camera.getInstance().getLeft() + Camera.getInstance().GL_WIDTH*2 - mGLWidth * 0.5f, location.y);
			if(Camera.getInstance().getBottom() + Camera.getInstance().GL_HEIGHT*2 - mGLHeight * 0.5f < location.y) // Top
				location.set(location.x, Camera.getInstance().getBottom() + Camera.getInstance().GL_HEIGHT*2 - mGLHeight * 0.5f);
			lastUpdate = t;
		}
		
		public void draw(GL10 gl){
			long time = GameClock.getInstance().getTime();
			switch(state){
			case 0:
				down.draw(gl, location, time);
				break;
			case 1:
				up.draw(gl, location, time);
				break;
			case 2: 
				left.draw(gl, location, time);
				break;
			case 3:
				right.draw(gl, location, time);
				break;
				
			}
		}
		
		public void setMoveState(int state) { moveState = state; }
		public int getMoveState() { return moveState; }
}
